package com.llb.llbliving.commodity.service.impl;

import com.llb.llbliving.commodity.dao.SpuInfoDao;
import com.llb.llbliving.commodity.entity.SpuInfoEntity;
import com.llb.llbliving.commodity.vo.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llb.common.utils.PageUtils;
import com.llb.common.utils.Query;

import com.llb.llbliving.commodity.dao.SkuInfoDao;
import com.llb.llbliving.commodity.entity.SkuInfoEntity;
import com.llb.llbliving.commodity.service.SkuInfoService;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Resource
    private SkuInfoService skuInfoService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();//带上查询条件
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }
//带上分类
        String catelogId = (String) params.get("catalogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
//带上品牌
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){

            queryWrapper.eq("brand_id",brandId);
        }
//价格范围
        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(min)){
            queryWrapper.ge("price",min);
        }
        String max = (String) params.get("max");
//校验传递的价格范围合理性, 如果 max 有值,并且大于 0，
//才有必要封装到查询条件
        if(!StringUtils.isEmpty(max) ){
            try{
                BigDecimal bigDecimal = new BigDecimal(max);
                if(bigDecimal.compareTo(new BigDecimal("0"))==1){queryWrapper.le("price",max);
                }
            }catch (Exception e){

            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params), queryWrapper
        );
        return new PageUtils(page);
    }

    @Resource
    private SpuInfoDao spuInfoDao;

    //返回购买用户检索的结果
    @Override
    public SearchResult querySearchPageByCondition(Map<String, Object> params) {
        params.put("limit", "2");
        //先查询已经上架的spuInfoEntity
        List<SpuInfoEntity> spuInfoEntities = spuInfoDao.selectList(new QueryWrapper<SpuInfoEntity>().eq("publish_status", 1));
        //收集已经上架的id到集合
        List<Long> spuInfoEntitiesIds = spuInfoEntities.stream().map(obj -> {
            return obj.getId();
        }).collect(Collectors.toList());


        //构建QueryWrapper
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        if(spuInfoEntitiesIds!=null&&spuInfoEntitiesIds.size()>0){
            wrapper.in("spu_id",spuInfoEntitiesIds);
        }else{
            //如果没有上架的商品，就构建一个SearchResult返回即可
            SearchResult searchResult = new SearchResult();
            searchResult.setPageNum(1);
            searchResult.setTotal(0l);
            searchResult.setTotalPages(0);
            searchResult.setPageNavs(new ArrayList<>());
            searchResult.setCommodity(new ArrayList<>());
            //设置keyword
            searchResult.setKeyword(params.get("keyword")==null?"":params.get("keyword").toString());
            return searchResult;
        }

        String keyword = (String)params.get("keyword");
        if(StringUtils.isNotBlank(keyword)){
            wrapper.and(w->{
                w.eq("sku_id",keyword).or().like("sku_name",keyword);
            });
        }

        String catalog3Id = (String)params.get("catalog3Id");
        if(StringUtils.isNotBlank(catalog3Id)&&!"0".equals(catalog3Id)){
            wrapper.eq("catalog_id",catalog3Id);
        }

        String brandId = (String)params.get("brandId");
        if(StringUtils.isNotBlank(brandId)&&!"0".equals(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );


        PageUtils pageUtils= new PageUtils(page);

        //将pageUtils相关的数据取出，根据业务需求进行二次封装
        SearchResult searchResult = new SearchResult();
        searchResult.setCommodity((List<SkuInfoEntity>) pageUtils.getList());
        searchResult.setTotalPages(pageUtils.getTotalPage());
        searchResult.setTotal((long) pageUtils.getTotalCount());
        searchResult.setPageNum(pageUtils.getCurrPage());

        List<Integer> objects = new ArrayList<>();
        for(int i=0;i<=pageUtils.getTotalPage();i++){
            objects.add(i);
        }
        searchResult.setPageNavs(objects);
        searchResult.setKeyword
                (params.get("keyword")==null?"":params.get("keyword").toString());
        return searchResult;
    }
}